﻿using System;
using System.Collections.Generic;
using Ur;

namespace Pixels.Logic
{

    static public class World
    {
        // STATIC PUBLIC VARIABLES ARE WRONG AND YOU SHOULD FEEL WRONG (tm)
        // WELL YOU TRY MAKING A GAME IN 24 HOURS, MR. SOFTWARE ENGINEER
        static public Map Map;
        static public Colony Colony;
        static public ILogicFlow Logic;
        static public Rendering.PeopleDragger pd;

        internal static void create() {

            var g = new Logic.Generators.WorldGenerator();

            World.Map = g.generateRandomMap(100, 40, 40);

            //World.Map[22, 22].changeType(TerrainType.fertile);
            //World.Map[22, 23].changeType(TerrainType.fertile);
            //World.Map[12, 15].changeType(TerrainType.mushrooms);
            World.Map[22, 22].changeType(TerrainType.crystals);// DEBUG Guaranted nearby crystal - TODO Maybe remove
            //World.Map[12, 16].visibility = Visibility.visible;

            World.Colony = new Logic.Colony();
            World.Logic  = new BasicLogicFlow();

            World.Logic.addRule(new Rules.FoodAndStarvation());            
            World.Logic.addRule(new Rules.ScoutingRule());
            World.Logic.addRule(new Rules.ConstructionRule());
            World.Logic.addRule(new Rules.Research());
            World.Logic.addRule(new Rules.DailyMessageRules());
            World.Logic.addRule(new Rules.NightCombatRule());
            World.Logic.addRule(new Rules.WhenNothingHappens());
            
            var ifc = new Interface.Interface();

            var startX = 20; var startY = 20;

            var wr = new Rendering.WorldRenderer();
            wr.camera.coords = wr.camera.target = new Ur.crds2(startX, startY);
            wr.camera.zoom = 1;

            Colony.accumulatedResources.addPoolToThisPool(GlobalConsts.startingResources);

            //Free bunker
            World.Map[startX + 1, startY + 1].changeType(TerrainType.rocks);
            Colony.spawnStructure(StructureType.bunker, startX + 1, startY+1);
            foreach (var tile in Map.selectTiles(startX - 1, startY - 1, 3, 4)) { tile.visibility = Visibility.visible; };


            //Guaranted nearby soil
            var farmTile = Map.selectRing(startX, startY, 2).GetRandom();
            farmTile.changeType(TerrainType.fertile);

            farmTile = Map.selectRing(startX, startY, 3).GetRandom();
            farmTile.changeType(TerrainType.fertile);

            //Spawn
            World.Map[startX, startY].changeType(TerrainType.rocks);
            Colony.spawnStructure(StructureType.commandModule, startX, startY);

            for (int i = 0; i < 3; i++) Colony.spawnColonist(Colonist.Occupation.colonist);
            for (int i = 0; i < 6; i++) Colony.spawnColonist(Colonist.Occupation.soldier);//DEBUG - TODO reduce number later
            for (int i = 0; i < 2; i++) Colony.spawnColonist(Colonist.Occupation.technician);


            /*var */pd = new Rendering.PeopleDragger(wr); // after initial colonists
            
        }

        static public Technologies CurrentResearch = Technologies.none;

        static public event Action TurnStart;

        public static int DAY_INDEX = 1;
        public static bool RadarBuilt = false;

        internal static void startDay() {
            DAY_INDEX += 1;
            if (TurnStart != null) TurnStart();
        }

        internal static Interface.Tooltip generateTooltipForTile(Tile hilitTile) {

            if (hilitTile.visibility == Visibility.dark) return new Interface.Tooltip("Unknown region", "Unexplored regions");
            if (hilitTile.visibility == Visibility.discoverable) {
                if (hilitTile.discoveryHandle.colonists.Count > 0) {
                    return new Interface.Tooltip("Exploring region", hilitTile.discoveryHandle.colonists.Count.ToString() + " scouts exploring");
                } else {
                    return new Interface.Tooltip("Unexplored region", "Drag soldiers to scout");
                }
            }

            if (hilitTile.HasStructure) {
                // structure
                hilitTile.StructureOnTile.calculateEffects();
                var sname = hilitTile.StructureOnTile.getName();
                var desc = GlobalConsts.getStructureConsts(hilitTile.StructureOnTile.MyType).description;
                if (hilitTile.StructureOnTile.MyType == StructureType.constructionSite) {
                    if (hilitTile.StructureOnTile.AssignedColonists.Count == 0) desc = "Construction halted! Assign technicians to continue";
                    else desc = "Construction in progress";
                }
                return new Interface.Tooltip(sname, desc);
            } else { // terrain
                var t = GlobalConsts.getTerrainConsts(hilitTile.Type);
                return new Interface.Tooltip(t.name, "Empty terrain - can construct!");//TODO if Crystals - can't construct

            }

            return new Interface.Tooltip("", "");


        }
    }



}
